
;; -------- highlight uncommitted changes --------
(use-package diff-hl
  :diminish diff-hl-mode
  :hook
  ((after-init . global-diff-hl-mode)
   (dired-mode . diff-hl-dired-mode))
  :config
  (diff-hl-margin-mode)
  (setq diff-hl-margin-side 'right)
  ;; Integration with magit
  (with-eval-after-load 'magit
    (add-hook 'magit-post-refresh-hook
	      'diff-hl-magit-post-refresh)))

;; -------- Git related modes --------
;; (use-package gitattributes-mode)
(use-package gitconfig-mode
  :defer t)
(use-package gitignore-mode
  :defer t)

;; -------- (ma)git --------
(use-package magit
  :mode (("\\COMMIT_EDITMSG\\'" . text-mode)
         ("\\MERGE_MSG\\'" . text-mode))
  :init
  (setq magit-commit-ask-to-stage nil
        magit-diff-refine-hunk t)
  :custom
  (magit-auto-revert-mode nil)
  :config
  (defun p1uxtar-insert-date-magit-commit ()
    "Insert string for today's date in commit msg."
    (interactive)
    (insert (format-time-string "%F")))
  :bind
  (("C-x g" . magit-status)
   ("C-c ." . 'p1uxtar-insert-date-magit-commit)))

(with-eval-after-load 'magit
  (add-hook 'magit-pre-refresh-hook #'diff-hl-magit-pre-refresh)
  (add-hook 'magit-post-refresh-hook #'diff-hl-magit-post-refresh))

;; (use-package smerge-mode
;;   :ensure t
;;   :config
;;   (add-hook 'find-file-hook (lambda ()
;; 			      (save-excursion
;; 				(goto-char (point-min))
;; 				(when (re-search-forward "^<<<<<<< " nil t)
;; 				  (smerge-mode 1))))
;; 	    t))

;; Resolve diff3 conflicts
(use-package smerge-mode
  :ensure nil
  :diminish
  :bind
  (:map smerge-mode-map
	("C-c m" . smerge-mode-hydra/body))
  :hook
  ((find-file . (lambda ()
		  (save-excursion
		    (goto-char (point-min))
		    (when (re-search-forward "^<<<<<<< " nil t)
		      (smerge-mode 1)))))
   (magit-diff-visit-file . (lambda ()
			      (when smerge-mode
				(hydra-smerge/body))))))

(provide '+vcs)
